﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="RemoveWebParts.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.WebPart
{
    using Engine;

    using Entities;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Files;
    using SharePoint.Wss.WebParts;

    using Utils;

    [CollaborisClass(
        Title = "Removes either a list (or all) web parts from a specified web part page",
        ReleaseStatus = ReleaseStatus.Development,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Allows multiple web parts to be removed from a web part page.",
        ClassGuid = "{126E17A5-A8BA-4e1f-8A48-1BC341A7C663}"
        )]
    public class RemoveWebParts : WSSActionBase<WebPartListInfo>
    {
        /// <summary>
        /// Does the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus DoAction(SafContext context, WebPartListInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            
            // now lets go connect em up.
           // SPFile targetFile = SPFileHelper.GetFile(this.TargetWeb, entity.FolderUrl, entity.PageName);

            //ValidationHelper.VerifyObjectArgument(targetFile, "targetFile");

            SPWebPartHelper.RemoveWebParts(
                                TargetWeb,
                                entity.WebPartPageRelativeUrl,
                                entity.WebPartIds,
                                entity.PersonalizationScope,
                                entity.RemoveAll);

            this.Trace.TraceMethodEnd("DoAction");
            
            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">The <see cref="SafContext"/> passed in by the SAF Engine.</param>
        /// <param name="entity">The data entity to Validate.</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid; otherwise true.</returns>
        protected override bool IsValid(SafContext context, WebPartListInfo entity, 
            ref System.Collections.Generic.List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.WebPartPageRelativeUrl, "WebPartListInfo.WebPartPageRelativeUrl", ref errors);
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }
}